"""

{
  "mainTitle":"xxx"
  "mainContent":{
    "pingan" : {}
    "sanjiu" : {}
    "xunyiwenyao":{}
  }
}

"""
import json
import os

# 3个源都有的
file1 = '39_pingan_xywy.txt'

# 2个源有的
file2 = '39_pingan.txt'
file3 = '39_xywy.txt'
file4 = 'pingan_xywy.txt'

# 自己独有的
file5 = '39.txt'
file6 = 'pingan.txt'
file7 = 'xywy.txt'

json_path1 = 'D:/PycharmProjects/three_source_json_data/39/json_data/'
json_path2 = 'D:/PycharmProjects/three_source_json_data/pingan/json_data/'
json_path3 = 'D:/PycharmProjects/three_source_json_data/xywy/json_data/'

save_path1 = 'D:/PycharmProjects/three_source_json_data/z_fusion/39_pingan_xywy/'
save_path2 = 'D:/PycharmProjects/three_source_json_data/z_fusion/39_pingan/'
save_path3 = 'D:/PycharmProjects/three_source_json_data/z_fusion/39_xywy/'
save_path4 = 'D:/PycharmProjects/three_source_json_data/z_fusion/pingan_xywy/'
save_path5 = 'D:/PycharmProjects/three_source_json_data/z_fusion/39/'
save_path6 = 'D:/PycharmProjects/three_source_json_data/z_fusion/pingan'
save_path7 = 'D:/PycharmProjects/three_source_json_data/z_fusion/xywy'


def find_json_data(name, path):
    for filename in os.listdir(path):
        file_path = os.path.join(path, filename)
        with open(file_path, mode='r', encoding='utf-8') as f:
            data = json.load(f)
        if data['title'] == name:
            return data


if __name__ == '__main__':

    # 自己独有的
    with open(file5, encoding='utf-8', mode='r') as f:
        disease_list = f.read().splitlines()
    for index, name in enumerate(disease_list):
        print(index)
        data = find_json_data(name, json_path1)
        final_data = {
            "mainTitle": name,
            "mainContent": {
                "sanjiu": data,
                "pingan": {},
                "xunyiwenyao": {}
            }
        }
        with open(os.path.join(save_path5, str(index) + '.json'), encoding='utf-8', mode='w') as f2:
            json.dump(final_data, f2, ensure_ascii=False)

    with open(file6, encoding='utf-8', mode='r') as f:
        disease_list = f.read().splitlines()
    for index, name in enumerate(disease_list):
        print(index)
        data = find_json_data(name, json_path1)
        final_data = {
            "mainTitle": name,
            "mainContent": {
                "sanjiu": {},
                "pingan": data,
                "xunyiwenyao": {}
            }
        }
        with open(os.path.join(save_path6, str(index) + '.json'), encoding='utf-8', mode='w') as f2:
            json.dump(final_data, f2, ensure_ascii=False)

    with open(file7, encoding='utf-8', mode='r') as f:
        disease_list = f.read().splitlines()
    for index, name in enumerate(disease_list):
        print(index)
        data = find_json_data(name, json_path1)
        final_data = {
            "mainTitle": name,
            "mainContent": {
                "sanjiu": {},
                "pingan": {},
                "xunyiwenyao": data
            }
        }
        with open(os.path.join(save_path7, str(index) + '.json'), encoding='utf-8', mode='w') as f2:
            json.dump(final_data, f2, ensure_ascii=False)
